package com.alibaba.order.service.impl;

import com.alibaba.basic.util.LoginContext;
import com.alibaba.basic.util.PageList;
import com.alibaba.order.domain.OrderPetAcquisition;
import com.alibaba.order.dto.OrderDto;
import com.alibaba.order.mapper.OrderPetAcquisitionMapper;
import com.alibaba.order.query.OrderPetAcquisitionQuery;
import com.alibaba.order.service.IOrderPetAcquisitionService;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.org.mapper.ShopMapper;
import com.alibaba.pet.domain.Pet;
import com.alibaba.pet.domain.SearchMasterMsg;
import com.alibaba.pet.mapper.PetMapper;
import com.alibaba.pet.mapper.SearchMasterMsgMapper;
import com.alibaba.user.domain.Logininfo;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        // 判读登录员工是那种角色
        // 1.employee中的shopid为null就是平台管理员
        if (employee.getShopId() !=null){// 店铺人员
            Shop shop = shopMapper.findById(employee.getShopId());
            if(shop.getAdminId() != employee.getId()){// 就是店铺员工
                query.setEmpId(employee.getId());
            }
            // 店铺管理员
                query.setShopId(employee.getShopId());
        }
            // 平台管理员
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        // 修改订单信息 状态 金额 支付方式
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(orderDto.getId());
        order.setState(1);
        order.setPrice(orderDto.getMoney());
        order.setPaytype(orderDto.getPaytype());
        orderPetAcquisitionMapper.update(order);
        // 修改寻主消息
        SearchMasterMsg msg = searchMasterMsgMapper.findById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        //修改pet的成本价
        Pet pet = petMapper.findById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);
    }

    @Override
    public void delete(Long id) {
        // 修改订单
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(id);
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
      // 修改寻主消息
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
     // 删除pet对象
        petMapper.delete(order.getPetId());
    }
}
